package com.example.spareparts.controller;

import com.example.spareparts.util.QRCodeGenerator;
import org.springframework.web.bind.annotation.*;

import com.example.spareparts.entity.SparePart;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/qrcode")
public class QRCodeController {

    @GetMapping("/generate")
    public String generateQRCode(@RequestParam String content) {
        try {
            String qrCode = QRCodeGenerator.generateQRCode(content, 200, 200);
            return "data:image/png;base64," + qrCode;
        } catch (Exception e) {
            e.printStackTrace(); // 添加详细错误日志
            return "error";
        }
    }
    // 新增接口：根据 sparepartid 查询备件信息
    @GetMapping("/info")
    public Map<String, Object> getSparePartInfo(@RequestParam Long sparepartid) {
        SparePart sparePart = new SparePart();
        // 模拟从数据库获取数据
        sparePart.setSparepartid(sparepartid);
        sparePart.setModel("Model-" + sparepartid); // 示例数据
        sparePart.setSpecification("Spec-" + sparepartid); // 示例数据
        sparePart.setSupplierid(1000 + sparepartid); // 示例数据

        Map<String, Object> response = new HashMap<>();
        response.put("sparepartid", sparePart.getSparepartid());
        response.put("model", sparePart.getModel());
        response.put("specification", sparePart.getSpecification());
        response.put("supplierid", sparePart.getSupplierid());
        return response;
    }
}
